<template>
  <div class="set-wrapper">
    <el-form :inline="true">
      <el-form-item label="所属部门：">
        <el-select style="width: 150px;" clearable v-model="searchDep" placeholder="请选择">
          <el-option
            v-for="item in departments"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchItem(true)">查询</el-button>
        <el-button type="primary" @click="openItemDialog()">新增</el-button>
      </el-form-item>
    </el-form>
    <div>
      <el-table :height="tableHeight" :data="tableData" border style="width: 100%">
        <el-table-column label="序号" type="index" width="100" align="center"></el-table-column>
        <el-table-column label="所属部门" prop="deptName" min-width="100" align="center"></el-table-column>
        <el-table-column label="考核名称" min-width="180" align="left" header-align="center">
          <template slot-scope="scope">
            <div style="white-space: pre-line;">{{scope.row.name}}</div>
          </template>
        </el-table-column>
        <el-table-column label="考核内容描述" min-width="180" align="left" header-align="center">
          <template slot-scope="scope">
            <div style="white-space: pre-line;">{{scope.row.content}}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center"  width="180">
            <template slot-scope="scope">
              <el-button @click="openItemDialog(scope.row)" type="text" size="small">编辑</el-button>
              <el-button @click="removeItem(scope.row)" type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <el-dialog title="考核条目" :visible.sync="itemDialog" width="600px" :close-on-click-modal="false">
      <el-form :model="form" style="padding-right:100px;" label-width="120px">
        <el-form-item label="所属部门：">
          <el-select v-model="form.deptId" :disabled="formStatus === 'edit'" ref="labelSelected" placeholder="所属部门">
            <el-option
              v-for="item in departments"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="考核分值倍数：">
          <el-input-number v-model="form.scoresPower" :min="1" :max="10"></el-input-number>
        </el-form-item>
        <el-form-item label="考核名称：">
          <el-input type="textarea" :rows="5" placeholder="请输入考核名称" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="考核内容描述：">
          <el-input type="textarea" :rows="5" placeholder="请输入考核内容描述" v-model="form.content"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="itemDialog = false">取 消</el-button>
        <el-button type="primary" @click="addItem">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import dingMixin from  '@/mixins/ding'
  import formUtil from '@/utils/formUtil'

  export default {
    data () {
      return {
        pageSize: 10,
        page:1,
        totalPage: 0,
        formStatus: 'add',
        itemDialog: false,
        form: {
          deptId: '',
          deptName: '',
          scoresPower: 1,
          content:'' ,
          name: ''
        },
        formVerify: {
          name: '请输入考核名称',
          content:'请输入考核内容描述' ,
        },
        tableData: [],
        departments: [],
        searchDep: '',
        tableHeight: window.tableHeight
      }
    },
    mixins: [dingMixin],
    activated() {
      this.tableHeight = document.documentElement['clientHeight'] - 270
      window.onresize = () => {
        this.tableHeight = document.documentElement['clientHeight'] - 270
      }
    },
    async mounted(){
      this.departments = await this.$httpN.get('sys/dept/list')
      this.searchItem(true);
    },
    methods: {
      //获取小项列表
      async searchItem(init) {
        if(init) this.page = 1;
        let data = await this.$httpN.postB('pde/item/list', {
          'deptId': this.searchDep,
          'withPublic': false,
          'page': this.page,
          'limit': this.pageSize
        })
        this.tableData = data.list;
        this.totalPage = data.totalCount;
      },

      //添加小项
      async addItem() {
        if(!formUtil.verifyData(this.form, this.formVerify)) return;
        this.form.deptName = this.$refs['labelSelected'].selectedLabel;
        if(!await this.$httpN.postB('pde/item/save', this.form)) return;
        this.itemDialog = false;
        formUtil.submitSuccess('保存成功');
        this.searchItem();
      },

      //打开条目窗口
      openItemDialog(item) {
        this.itemDialog = true;
        if(item) {
          this.form = formUtil.deepCopy(item);
          this.formStatus = 'edit'
        }else {
          formUtil.resetData(this.form);
          if(this.searchDep) this.form.deptId = this.searchDep;
          this.formStatus = 'add'
        }
      },
      sizeChangeHandle(limit) {
        this.pageSize = limit;
        this.searchItem(true);
      },
      currentChangeHandle(page) {
        this.page = page;
        this.searchItem();
      },
      //删除条目
      removeItem(item) {
        formUtil.submitConfirm('此操作将永久删除该条目, 是否继续?', async ()=> {
          if(!await this.$httpN.postB('pde/item/delete', [item.id], false)) return;
          formUtil.submitSuccess('删除成功');
          this.searchItem()
        })
      }
    }
  }
</script>
